package com.yuneec.fly.views;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yuneec.fly.R;

public class FocusView extends ViewGroup {

    private int viewWidth;
    private int viewHeight;
    private OnFocusListener focusListener;
    private GestureDetector gestureDetector;
    private AnimatorSet animationSet;

    private final int focusViewWidth = 100;
    private final int focusViewHeight = 100;

    private ImageView focusView;
    private SoundPool soundPool;

    public enum PressEventType {
        SINGLE_TAP_EVENT, LONG_PRESS,
        EVENT_UP, EVENT_DOWN
    }

    ;

    public void setOnFocusListener(OnFocusListener listener) {
        focusListener = listener;
    }

    public interface OnFocusListener {
        public void onFocus(PressEventType pressEventType, float xRatio, float yRatio);
    }

    public FocusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(getContext(),
                new MyGestureListener());
        focusView = new ImageView(getContext());
        focusView.setImageResource(R.drawable.cgo4_focus_view);
        focusView.setVisibility(View.GONE);
        this.addView(focusView);


        soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
        soundPool.load(getContext(), R.raw.timer_final_second, 1);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
    }

    private float downEventX = 0;
    private float downEventY = 0;

    public float getXRatio() {
        return downEventX / (float) viewWidth;
    }

    public float getYRatio() {
        return downEventY / (float) viewHeight;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean isConsumed = gestureDetector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downEventX = event.getX();
                downEventY = event.getY();
                focusView.setTranslationX(event.getX() - focusView.getWidth() / 2);
                focusView.setTranslationY(event.getY() - focusView.getHeight() / 2);
                focusView.setVisibility(View.VISIBLE);
                startAnimation(400);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return isConsumed;
    }

    public void spreadTouchEvent(MotionEvent event) {
        if (focusListener != null) {
            float xRatio = downEventX / (float) viewWidth;
            float yRatio = downEventY / (float) viewHeight;
            focusListener.onFocus(PressEventType.EVENT_UP, xRatio, yRatio);
        }
//        soundPool.play(1,1, 1, 0, 0, 1);
        focusView.setTranslationX(downEventX - focusView.getWidth() / 2);
        focusView.setTranslationY(downEventY - focusView.getHeight() / 2);
        focusView.setVisibility(View.VISIBLE);
        startAnimation(400);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    private void terminalAnimation() {
        if (animationSet != null && animationSet.isRunning()) {
            animationSet.cancel();
            animationSet = null;
        }
    }

    private void startAnimation(long duration) {
        if (animationSet != null && animationSet.isRunning()) {
            return;
        }

        animationSet = new AnimatorSet();

        Animator firstAnimator = ObjectAnimator.ofFloat(focusView, "scaleX", 1.0f, 0.5f);
        animationSet.play(firstAnimator).with(ObjectAnimator.ofFloat(focusView, "scaleY", 1.0f, 0.5f));
        animationSet.play(ObjectAnimator.ofFloat(focusView, "scaleX", 0.5f, 0.7f)).with(ObjectAnimator.ofFloat(focusView, "scaleY", 0.5f, 0.7f)).after(firstAnimator);
        animationSet.setDuration(duration);
        animationSet.addListener(new AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                focusView.setScaleX(1.0f);
                focusView.setScaleY(1.0f);
                focusView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                focusView.setScaleX(1.0f);
                focusView.setScaleY(1.0f);
                focusView.setVisibility(View.GONE);
            }
        });
        animationSet.start();
    }

    private class MyGestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            float xRatio = e.getX() / (float) viewWidth;
            float yRatio = e.getY() / (float) viewHeight;
            if (focusListener != null) {
                focusListener.onFocus(PressEventType.EVENT_UP, xRatio, yRatio);
            }
            if (click != null) {
                click.onClick(FocusView.this, xRatio, yRatio);
            }
            soundPool.play(1, 1, 1, 0, 0, 1);
            return true;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            if (focusListener != null) {
                float xRatio = e.getX() / (float) viewWidth;
                float yRatio = e.getY() / (float) viewHeight;
                focusListener.onFocus(PressEventType.EVENT_DOWN, xRatio, yRatio);
            }
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }

        public void onLongPress(MotionEvent e) {
            float xRatio = e.getX() / (float) viewWidth;
            float yRatio = e.getY() / (float) viewHeight;
            if (focusListener != null) {
                focusListener.onFocus(PressEventType.LONG_PRESS, xRatio, yRatio);
            }
            if (longClick != null) {
                longClick.onLongClick(FocusView.this, xRatio, yRatio);
            }
            soundPool.play(1, 1, 1, 0, 0, 1);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final ImageView focusView = (ImageView) getChildAt(0);
        focusView.layout(0, 0, focusViewWidth, focusViewHeight);
    }

    private OnClick click;
    private OnLongClick longClick;

    public void setOnViewClick(OnClick click) {
        this.click = click;
    }

    public void setOnViewLongClick(OnLongClick longClick) {
        this.longClick = longClick;
    }

    public static interface OnClick {
        public void onClick(FocusView focusView, float xRatio, float yRatio);
    }

    public static interface OnLongClick {
        public void onLongClick(FocusView focusView, float xRatio, float yRatio);
    }
}
